namespace Romer
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;

    internal class NdsIcon
    {
        private byte[] bNDSCountry = new byte[4];
        private byte[] bNDSCrc = new byte[2];
        private byte[] bNDSPalette = new byte[0x20];
        private byte[] bNDSROMTitle = new byte[12];
        private byte[] bNDSTile = new byte[0x200];
        private byte[] bNDSTitle = new byte[0x100];
        private byte[] bNDSVersion = new byte[2];
        public byte[] BoundArray = new byte[4];
        public bool Homebrew;
        public string NdsFile;
        public int SeekByte;
        private Color transparent = new Color();

        public bool CheckHeaderCRC()
        {
            byte[] p = new byte[350];
            byte[] buffer2 = new byte[2];
            FileStream input = new FileStream(this.NdsFile, FileMode.Open);
            BinaryReader reader = new BinaryReader(input) {
                BaseStream = { Position = 0L }
            };
            p = reader.ReadBytes(350);
            reader.BaseStream.Position = 350L;
            buffer2 = reader.ReadBytes(2);
            input.Close();
            CRCTool tool = new CRCTool();
            tool.Init(CRCTool.CRCCode.CRC16);
            ulong num = BitConverter.ToUInt16(BitConverter.GetBytes(tool.crcbitbybit(p)), 0);
            ulong num2 = BitConverter.ToUInt16(buffer2, 0);
            return (num == num2);
        }

        public int ConvertToUInt4(string input)
        {
            int num = 0;
            for (int i = 0; i < 4; i++)
            {
                int num2 = int.Parse(input.Substring(3 - i, 1));
                num += num2 * Convert.ToInt16(Math.Pow(2.0, (double) i));
            }
            return num;
        }

        public int ConvertToUInt8(string input)
        {
            int num = 0;
            for (int i = 0; i < 8; i++)
            {
                int num2 = int.Parse(input.Substring(i, 1));
                num += num2 * (2 ^ i);
            }
            return num;
        }

        public void GetBounds()
        {
            FileStream input = new FileStream(this.NdsFile, FileMode.Open);
            this.BoundArray = new BinaryReader(input) { BaseStream = { Position = 0x68L } }.ReadBytes(4);
            input.Close();
            Console.WriteLine("BREAK: GETBOUNDS");
        }

        public string GetCountryCode()
        {
            FileStream input = new FileStream(this.NdsFile, FileMode.Open);
            this.bNDSCountry = new BinaryReader(input) { BaseStream = { Position = 12L } }.ReadBytes(4);
            string str = Encoding.UTF8.GetString(this.bNDSCountry);
            input.Close();
            Console.WriteLine("BREAK: GETTILE");
            return str;
        }

        public uint GetCRC()
        {
            if (this.CheckHeaderCRC())
            {
                FileStream input = new FileStream(this.NdsFile, FileMode.Open);
                BinaryReader reader = new BinaryReader(input);
                this.SeekByte = BitConverter.ToInt32(this.BoundArray, 0);
                reader.BaseStream.Position = this.SeekByte + 2;
                this.bNDSCrc = reader.ReadBytes(2);
                input.Close();
                return BitConverter.ToUInt16(this.bNDSCrc, 0);
            }
            return 0;
        }

        public Bitmap GetIcon(out Color[] palette)
        {
            Color[] colorArray = new Color[0x10];
            Bitmap bitmap = new Bitmap(0x20, 0x20);
            int index = 0;
            try
            {
                this.Homebrew = false;
                for (int i = 0; i < 0x20; i++)
                {
                    string str = Convert.ToString((int) BitConverter.ToUInt16(this.bNDSPalette, i), 2).PadLeft(0x10, '0');
                    i++;
                    decimal num3 = Convert.ToUInt32(str.Substring(11, 5), 2);
                    decimal num4 = Convert.ToUInt32(str.Substring(6, 5), 2);
                    decimal num5 = Convert.ToUInt32(str.Substring(1, 5), 2);
                    int red = Convert.ToInt16((decimal) ((num3 / 31M) * 255M));
                    int green = Convert.ToInt16((decimal) ((num4 / 31M) * 255M));
                    int blue = Convert.ToInt16((decimal) ((num5 / 31M) * 255M));
                    colorArray[index] = Color.FromArgb(0xff, red, green, blue);
                    index++;
                }
                index = 0;
                for (int j = 0; j < 0x10; j++)
                {
                    for (int k = (j / 4) * 8; k < (((j / 4) * 8) + 8); k++)
                    {
                        for (int m = (j % 4) * 8; m < (((j % 4) * 8) + 8); m++)
                        {
                            int num12 = 0;
                            if ((m % 2) == 1)
                            {
                                if ((this.bNDSTile[index] > 1) && (index < 0x200))
                                {
                                    string input = Convert.ToString((int) BitConverter.ToUInt16(this.bNDSTile, index), 2).PadLeft(0x10, '0').Substring(8, 4);
                                    num12 = this.ConvertToUInt4(input);
                                }
                                index++;
                            }
                            else if (index < 0x200)
                            {
                                string str5 = Convert.ToString((int) BitConverter.ToUInt16(this.bNDSTile, index), 2).PadLeft(0x10, '0').Substring(12, 4);
                                num12 = this.ConvertToUInt4(str5);
                            }
                            bitmap.SetPixel(m, k, colorArray[num12]);
                            if ((m == 4) & (k == 0))
                            {
                                this.transparent = colorArray[num12];
                            }
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("ERROR: Pallete Invalid..Probably Homebrew", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.Homebrew = true;
            }
            palette = colorArray;
            return bitmap;
        }
        public Bitmap GetIcon()
        {
            Color[] colorArray = new Color[0x10];
            Bitmap bitmap = new Bitmap(0x20, 0x20);
            int index = 0;
            try
            {
                this.Homebrew = false;
                for (int i = 0; i < 0x20; i++)
                {
                    string str = Convert.ToString((int)BitConverter.ToUInt16(this.bNDSPalette, i), 2).PadLeft(0x10, '0');
                    i++;
                    decimal num3 = Convert.ToUInt32(str.Substring(11, 5), 2);
                    decimal num4 = Convert.ToUInt32(str.Substring(6, 5), 2);
                    decimal num5 = Convert.ToUInt32(str.Substring(1, 5), 2);
                    int red = Convert.ToInt16((decimal)((num3 / 31M) * 255M));
                    int green = Convert.ToInt16((decimal)((num4 / 31M) * 255M));
                    int blue = Convert.ToInt16((decimal)((num5 / 31M) * 255M));
                    colorArray[index] = Color.FromArgb(0xff, red, green, blue);
                    index++;
                }
                index = 0;
                for (int j = 0; j < 0x10; j++)
                {
                    for (int k = (j / 4) * 8; k < (((j / 4) * 8) + 8); k++)
                    {
                        for (int m = (j % 4) * 8; m < (((j % 4) * 8) + 8); m++)
                        {
                            int num12 = 0;
                            if ((m % 2) == 1)
                            {
                                if ((this.bNDSTile[index] > 1) && (index < 0x200))
                                {
                                    string input = Convert.ToString((int)BitConverter.ToUInt16(this.bNDSTile, index), 2).PadLeft(0x10, '0').Substring(8, 4);
                                    num12 = this.ConvertToUInt4(input);
                                }
                                index++;
                            }
                            else if (index < 0x200)
                            {
                                string str5 = Convert.ToString((int)BitConverter.ToUInt16(this.bNDSTile, index), 2).PadLeft(0x10, '0').Substring(12, 4);
                                num12 = this.ConvertToUInt4(str5);
                            }
                            bitmap.SetPixel(m, k, colorArray[num12]);
                            if ((m == 4) & (k == 0))
                            {
                                this.transparent = colorArray[num12];
                            }
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("ERROR: Pallete Invalid..Probably Homebrew", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.Homebrew = true;
            }
            return bitmap;
        }
        public int GetIdex(string LanguageSelect)
        {
            switch (LanguageSelect)
            {
                case "Jap":
                    return 1;

                case "Eng":
                    return 2;

                case "Fre":
                    return 3;

                case "Ger":
                    return 4;

                case "Ita":
                    return 5;

                case "Spa":
                    return 6;
            }
            return 2;
        }

        public void GetPallete()
        {
            FileStream input = new FileStream(this.NdsFile, FileMode.Open);
            BinaryReader reader = new BinaryReader(input);
            this.SeekByte = BitConverter.ToInt32(this.BoundArray, 0);
            reader.BaseStream.Position = this.SeekByte + 0x220;
            this.bNDSPalette = reader.ReadBytes(0x20);
            input.Close();
            Console.WriteLine("BREAK: GETPALETTE");
        }

        public string GetROMTitle()
        {
            FileStream input = new FileStream(this.NdsFile, FileMode.Open);
            this.bNDSROMTitle = new BinaryReader(input) { BaseStream = { Position = 0L } }.ReadBytes(12);
            string str = Encoding.UTF8.GetString(this.bNDSROMTitle);
            input.Close();
            Console.WriteLine("BREAK: GETTILE");
            return str;
        }

        public void GetTile()
        {
            FileStream input = new FileStream(this.NdsFile, FileMode.Open);
            BinaryReader reader = new BinaryReader(input);
            this.SeekByte = BitConverter.ToInt32(this.BoundArray, 0);
            reader.BaseStream.Position = this.SeekByte + 0x20;
            this.bNDSTile = reader.ReadBytes(0x201);
            input.Close();
            Console.WriteLine("BREAK: GETTILE");
        }

        public string GetTitle(int language)
        {
            int num = 0x240 + (0x100 * (language - 1));
            FileStream input = new FileStream(this.NdsFile, FileMode.Open);
            BinaryReader reader = new BinaryReader(input);
            this.SeekByte = BitConverter.ToInt32(this.BoundArray, 0);
            reader.BaseStream.Position = this.SeekByte + num;
            this.bNDSTitle = reader.ReadBytes(0x100);
            string str = this.U16ToString(this.bNDSTitle);
            input.Close();
            Console.WriteLine("BREAK: GETTITLE");
            return str;
        }

        public string GetVersion()
        {
            FileStream input = new FileStream(this.NdsFile, FileMode.Open);
            BinaryReader reader = new BinaryReader(input);
            this.SeekByte = BitConverter.ToInt32(this.BoundArray, 0);
            reader.BaseStream.Position = this.SeekByte;
            this.bNDSVersion = reader.ReadBytes(3);
            string str = Convert.ToString(BitConverter.ToInt16(this.bNDSVersion, 0));
            input.Close();
            Console.WriteLine("BREAK: GETVER");
            return str;
        }

        public byte[] ReadRawIcon()
        {
            byte[] buffer = new byte[0x840];
            this.GetBounds();
            FileStream input = new FileStream(this.NdsFile, FileMode.Open);
            buffer = new BinaryReader(input) { BaseStream = { Position = (long) BitConverter.ToInt32(this.BoundArray, 0) } }.ReadBytes(0x840);
            input.Close();
            return buffer;
        }

        public string U16ToString(byte[] Input)
        {
            try
            {
                System.Text.Decoder decoder = new UnicodeEncoding(false, true, true).GetDecoder();
                char[] chars = new char[decoder.GetCharCount(Input, 0, Input.Length, true)];
                decoder.GetChars(Input, 0, Input.Length, chars, 0, true);
                return new string(chars);
            }
            catch
            {
                MessageBox.Show("ERROR: Header Invalid..Probably Homebrew", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.Homebrew = true;
                return "";
            }
        }
    }
}

